﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KingSite.SyncDB.Core.Repository;
using KingSite.SyncDB.Core.Entity;
using System.Data.SqlClient;

namespace KingSite.SyncDBManager {
    public partial class DBSetupFrm : Form {
        DBRepository dbr = new DBRepository();
        DBTypeRepository dbtr = new DBTypeRepository();

        public DBSetupFrm() {
            InitializeComponent();
        }

        private void DBSetupFrm_Load(object sender, EventArgs e) {
            Init();
        }

        private void Init() {
            IList<DBTypeEntity> list = dbtr.GetAllDBType();
            list.Insert(0,new DBTypeEntity { Id = 0, DBTypeName = "请选择" });
            CB_DBName.DataSource = list;            
            CB_DBName.DisplayMember = "DBTypeName";
            CB_DBName.ValueMember = "Id";
            BindData();

        }

        private void BindData() {
            IList<DBEntity> alldb = dbr.GetAllDBInfo();
            BindingSource source = new BindingSource();
            source.DataSource = alldb;
            bindingNavigator1.BindingSource = source;
            dataGridView1.DataSource = source;
            if (alldb.Count > 0) {
                SetDBEntity(1);
            }

        }

        private void SetDBEntity(int id) {
            DBEntity entity = dbr.GetDBEntity(id);
            Lbl_DBId.Text = id.ToString();
            Txt_DBName.Text = entity.DbName;
            Txt_DBConnString.Text = entity.DbConnString;
            CB_DBName.SelectedIndex = entity.DbTypeId;     
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
            if (dataGridView1.SelectedCells.Count != 0) {
                //得到选中行某列的值
                int id = Convert.ToInt32(dataGridView1.CurrentRow.Cells["Id"].Value);
                SetDBEntity(id);                
            }
        }

        private void Btn_Save_Click(object sender, EventArgs e) {
            if (CB_DBName.SelectedIndex == 0) {
                MessageBox.Show("请选择数据库类型！");
                return;
            }
            DBEntity entity = new DBEntity();
            entity.DbName = Txt_DBName.Text;
            entity.DbConnString = Txt_DBConnString.Text;
            entity.DbTypeId = CB_DBName.SelectedIndex;
            if (!string.IsNullOrEmpty(Lbl_DBId.Text)) {
                entity.Id = Convert.ToInt32(Lbl_DBId.Text);
                dbr.UpdateDBEntity(entity);
            }
            else {
                dbr.InsertDBEntity(entity);
            }

            BindData();
        }

        private void Btn_New_Click(object sender, EventArgs e) {
            Lbl_DBId.Text = "";
            Txt_DBName.Text = "";
            Txt_DBConnString.Text = "";
            CB_DBName.SelectedIndex = 0;
        }

        private void Btn_Del_Click(object sender, EventArgs e) {
            if (MessageBox.Show("确定删除么？", "确认删除", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes) {
                int id = Convert.ToInt32(Lbl_DBId.Text);
                dbr.DeleteDBEntity(id);
                BindData();
            }
            
        }

        private void Btn_ConnTest_Click(object sender, EventArgs e) {            
            string connstr = Txt_DBConnString.Text;
            if (string.IsNullOrEmpty(connstr)) {
                MessageBox.Show("请输入连接字符串！");
                return;
            }
            if (CB_DBName.SelectedIndex == 0) {
                MessageBox.Show("请选择数据库类型！");
                return;
            }
            DBTypeEnum dbtype = (DBTypeEnum)CB_DBName.SelectedIndex;
            if (dbtype == DBTypeEnum.SQLSERVER) {
                try {
                    using (SqlConnection conn = new SqlConnection(connstr)) {
                        conn.Open();
                        conn.Close();
                        MessageBox.Show("数据库连接成功！");                        
                    }
                }
                catch (Exception ex) {
                    MessageBox.Show("数据库连接失败！" + ex.Message);
                }
            }
            else if (dbtype == DBTypeEnum.MYSQL) {
                MessageBox.Show("暂时不支持 MySql 类型数据库！");
            }
            else if (dbtype == DBTypeEnum.ORACLE) {
                MessageBox.Show("暂时不支持 Oracle 类型数据库！");
            }
        }


    }
}
